package com.lin.common.utils;

import com.lin.common.exception.BaseException;
import com.lin.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;


@Slf4j

public class XmlUtil {

    @Value("${alioss.endpoint}")
    private String endpoint = "oss-cn-beijing.aliyuncs.com";
    @Value("${alioss.accessKeyId}")
    private String accessKeyId = "LTAI5tLFoZFWxTGXHiNCX5kc";
    @Value("${alioss.accessKeySecret}")
    private String accessKeySecret = "D37SWz6TIrea9dR8T1yMrCbPWTzyNJ";
    @Value("${alioss.bucketName}")
    private String bucketName = "piano-oss";
    public String setXml(String fileName,int[] nodes) {

        // 2.创建dom对象
        Document document = DocumentHelper.createDocument();

        // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
        //曲谱
        Element Music = document.addElement("Music");
        for (int i = 0; i < nodes.length; i++) {
            //音节
            Element Syllable = Music.addElement("Syllable");
            //发音
            Element Pronounce = Syllable.addElement("Pronounce");
            Element audio = Syllable.addElement("audio");
            Element time = Syllable.addElement("time");
            switch (nodes[i]) {
                case 0:
                    Pronounce.addText("zero");
                    audio.addText("0");
                    time.addText("0");
                    break;
                case 1:
                    Pronounce.addText("C");
                    audio.addText("audio/m1.mp3");
                    time.addText("1");
                    break;
                case 2:
                    Pronounce.addText("D");
                    audio.addText("audio/m2.mp3");
                    time.addText("1");
                    break;
                case 3:
                    Pronounce.addText("E");
                    audio.addText("audio/m3.mp3");
                    time.addText("1");
                    break;
                case 4:
                    Pronounce.addText("F");
                    audio.addText("audio/m4.mp3");
                    time.addText("1");
                    break;
                case 5:
                    Pronounce.addText("G");
                    audio.addText("audio/m5.mp3");
                    time.addText("1");
                    break;
                case 6:
                    Pronounce.addText("A");
                    audio.addText("audio/m6.mp3");
                    time.addText("1");
                    break;
                case 7:
                    Pronounce.addText("B");
                    audio.addText("audio/m7.mp3");
                    time.addText("1");
                    break;
                case 8:
                    Pronounce.addText("hC");
                    audio.addText("audio/h1.mp3");
                    time.addText("1");
                    break;
                case 9:
                    Pronounce.addText("hD");
                    audio.addText("audio/h2.mp3");
                    time.addText("1");
                    break;
                case 10:
                    Pronounce.addText("hE");
                    audio.addText("audio/h3.mp3");
                    time.addText("1");
                    break;
                case 11:
                    Pronounce.addText("hF");
                    audio.addText("audio/h4.mp3");
                    time.addText("1");
                    break;
                case 12:
                    Pronounce.addText("hG");
                    audio.addText("audio/h5.mp3");
                    time.addText("1");
                    break;
                case 13:
                    Pronounce.addText("hA");
                    audio.addText("audio/h6.mp3");
                    time.addText("1");
                    break;
                case 14:
                    Pronounce.addText("hB");
                    audio.addText("audio/h7.mp3");
                    time.addText("1");
                    break;
                case 15:
                    Pronounce.addText("lC");
                    audio.addText("audio/l1.mp3");
                    time.addText("1");
                    break;
                case 16:
                    Pronounce.addText("lD");
                    audio.addText("audio/l2.mp3");
                    time.addText("1");
                    break;
                case 17:
                    Pronounce.addText("lE");
                    audio.addText("audio/l3.mp3");
                    time.addText("1");
                    break;
                case 18:
                    Pronounce.addText("lF");
                    audio.addText("audio/l4.mp3");
                    time.addText("1");
                    break;
                case 19:
                    Pronounce.addText("lG");
                    audio.addText("audio/l5.mp3");
                    time.addText("1");
                    break;
                case 20:
                    Pronounce.addText("lA");
                    audio.addText("audio/l6.mp3");
                    time.addText("1");
                    break;
                case 21:
                    Pronounce.addText("lB");
                    audio.addText("audio/l7.mp3");
                    time.addText("1");
                    break;
                case 22:
                    Pronounce.addText("--");
                    audio.addText("audio/pause.mp3");
                    time.addText("1");
                    break;
                case 23:
                    Pronounce.addText("C·");
                    audio.addText("audio/m1.mp3");
                    time.addText("0.5");
                    break;
                case 24:
                    Pronounce.addText("D·");
                    audio.addText("audio/m2.mp3");
                    time.addText("0.5");
                    break;
                case 25:
                    Pronounce.addText("E·");
                    audio.addText("audio/m3.mp3");
                    time.addText("0.5");
                    break;
                case 26:
                    Pronounce.addText("F·");
                    audio.addText("audio/m4.mp3");
                    time.addText("0.5");
                    break;
                case 27:
                    Pronounce.addText("G·");
                    audio.addText("audio/m5.mp3");
                    time.addText("0.5");
                    break;
                case 28:
                    Pronounce.addText("A·");
                    audio.addText("audio/m6.mp3");
                    time.addText("0.5");
                    break;
                case 29:
                    Pronounce.addText("B·");
                    audio.addText("audio/m7.mp3");
                    time.addText("0.5");
                    break;
                case 30:
                    Pronounce.addText("C_");
                    audio.addText("audio/m1.mp3");
                    time.addText("0.5");
                    break;
                case 31:
                    Pronounce.addText("D_");
                    audio.addText("audio/m2.mp3");
                    time.addText("0.5");
                    break;
                case 32:
                    Pronounce.addText("E_");
                    audio.addText("audio/m3.mp3");
                    time.addText("0.5");
                    break;
                case 33:
                    Pronounce.addText("F_");
                    audio.addText("audio/m4.mp3");
                    time.addText("0.5");
                    break;
                case 34:
                    Pronounce.addText("G_");
                    audio.addText("audio/m5.mp3");
                    time.addText("0.5");
                    break;
                case 35:
                    Pronounce.addText("A_");
                    audio.addText("audio/m6.mp3");
                    time.addText("0.5");
                    break;
                case 36:
                    Pronounce.addText("B_");
                    audio.addText("audio/m7.mp3");
                    time.addText("0.5");
                    break;
                case 37:
                    Pronounce.addText("hC_");
                    audio.addText("audio/h1.mp3");
                    time.addText("0.5");
                    break;
                case 38:
                    Pronounce.addText("hD_");
                    audio.addText("audio/h2.mp3");
                    time.addText("0.5");
                    break;
                case 39:
                    Pronounce.addText("hE_");
                    audio.addText("audio/h3.mp3");
                    time.addText("0.5");
                    break;
                case 40:
                    Pronounce.addText("hF_");
                    audio.addText("audio/h4.mp3");
                    time.addText("0.5");
                    break;
                case 41:
                    Pronounce.addText("hG_");
                    audio.addText("audio/h5.mp3");
                    time.addText("0.5");
                    break;
                case 42:
                    Pronounce.addText("hA_");
                    audio.addText("audio/h6.mp3");
                    time.addText("0.5");
                    break;
                case 43:
                    Pronounce.addText("hB_");
                    audio.addText("audio/h7.mp3");
                    time.addText("0.5");
                    break;
                case 44:
                    Pronounce.addText("lC_");
                    audio.addText("audio/l1.mp3");
                    time.addText("0.5");
                    break;
                case 45:
                    Pronounce.addText("lD_");
                    audio.addText("audio/l2.mp3");
                    time.addText("0.5");
                    break;
                case 46:
                    Pronounce.addText("lE_");
                    audio.addText("audio/l3.mp3");
                    time.addText("0.5");
                    break;
                case 47:
                    Pronounce.addText("lF_");
                    audio.addText("audio/l4.mp3");
                    time.addText("0.5");
                    break;
                case 48:
                    Pronounce.addText("lG_");
                    audio.addText("audio/l5.mp3");
                    time.addText("0.5");
                    break;
                case 49:
                    Pronounce.addText("lA_");
                    audio.addText("audio/l6.mp3");
                    time.addText("0.5");
                    break;
                case 50:
                    Pronounce.addText("lB_");
                    audio.addText("audio/l7.mp3");
                    time.addText("0.5");
                    break;
                case 51:
                    Pronounce.addText("C_2");
                    audio.addText("audio/m1.mp3");
                    time.addText("0.25");
                    break;
                case 52:
                    Pronounce.addText("D_2");
                    audio.addText("audio/m2.mp3");
                    time.addText("0.25");
                    break;
                case 53:
                    Pronounce.addText("E_2");
                    audio.addText("audio/m3.mp3");
                    time.addText("0.25");
                    break;
                case 54:
                    Pronounce.addText("F_2");
                    audio.addText("audio/m4.mp3");
                    time.addText("0.25");
                    break;
                case 55:
                    Pronounce.addText("G_2");
                    audio.addText("audio/m5.mp3");
                    time.addText("0.25");
                    break;
                case 56:
                    Pronounce.addText("A_2");
                    audio.addText("audio/m6.mp3");
                    time.addText("0.25");
                    break;
                case 57:
                    Pronounce.addText("B_2");
                    audio.addText("audio/m7.mp3");
                    time.addText("0.25");
                    break;
                case 58:
                    Pronounce.addText("hC_2");
                    audio.addText("audio/h1.mp3");
                    time.addText("0.25");
                    break;
                case 59:
                    Pronounce.addText("hD_2");
                    audio.addText("audio/h2.mp3");
                    time.addText("0.25");
                    break;
                case 60:
                    Pronounce.addText("hE_2");
                    audio.addText("audio/h3.mp3");
                    time.addText("0.25");
                    break;
                case 61:
                    Pronounce.addText("hF_2");
                    audio.addText("audio/h4.mp3");
                    time.addText("0.25");
                    break;
                case 62:
                    Pronounce.addText("hG_2");
                    audio.addText("audio/h5.mp3");
                    time.addText("0.25");
                    break;
                case 63:
                    Pronounce.addText("hA_2");
                    audio.addText("audio/h6.mp3");
                    time.addText("0.25");
                    break;
                case 64:
                    Pronounce.addText("hB_2");
                    audio.addText("audio/h7.mp3");
                    time.addText("0.25");
                    break;
                case 65:
                    Pronounce.addText("lC_2");
                    audio.addText("audio/l1.mp3");
                    time.addText("0.25");
                    break;
                case 66:
                    Pronounce.addText("lD_2");
                    audio.addText("audio/l2.mp3");
                    time.addText("0.25");
                    break;
                case 67:
                    Pronounce.addText("lE_2");
                    audio.addText("audio/l3.mp3");
                    time.addText("0.25");
                    break;
                case 68:
                    Pronounce.addText("lF_2");
                    audio.addText("audio/l4.mp3");
                    time.addText("0.25");
                    break;
                case 69:
                    Pronounce.addText("lG_2");
                    audio.addText("audio/l5.mp3");
                    time.addText("0.25");
                    break;
                case 70:
                    Pronounce.addText("lA_2");
                    audio.addText("audio/l6.mp3");
                    time.addText("0.25");
                    break;
                case 71:
                    Pronounce.addText("lB_2");
                    audio.addText("audio/l7.mp3");
                    time.addText("0.25");
                    break;
                case 72:
                    Pronounce.addText("分隔符");
                    audio.addText("1");
                    time.addText("1");
                    break;
                case 73:
                    Pronounce.addText("zero_");
                    audio.addText("0");
                    time.addText("0.5");
                    break;
                case 74:
                    Pronounce.addText("zero_2");
                    audio.addText("0");
                    time.addText("0.25");
                    break;
                default:
                    break;
            }
        }
        // 4、格式化模板
        //OutputFormat format = OutputFormat.createCompactFormat();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        // 指定文件路径
        String fileLocation = "musicXml/";
        File file = new File(fileLocation+fileName + ".xml");
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs(); // 创建文件夹及其父文件夹
        }
        // 5、生成xml文件
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            XMLWriter writer = new XMLWriter(out, format);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            System.out.println("生成xml文件失败。文件名【" + fileName + "】");
        }

        // 6、生成的XML文件
        // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将文件内容转换为字节数组
        byte[] fileBytes;
        try {
            fileBytes = Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            throw new BaseException("读取文件失败");
        }

        // 上传到阿里云 OSS
        AliOssUtil aliOssUtil = new AliOssUtil(endpoint,accessKeyId,accessKeySecret,bucketName);
        String objectName = fileName + ".xml"; // OSS 中的对象名称
        String fileUrl = aliOssUtil.upload(fileBytes, objectName); // 上传并获取访问路径

        return fileUrl;
    }
}

